package net.maku.member.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.member.convert.MemberDeptConvert;
import net.maku.member.entity.MemberDeptEntity;
import net.maku.member.service.MemberDeptService;
import net.maku.member.query.MemberDeptQuery;
import net.maku.member.vo.MemberDeptVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 部门管理
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-15
*/
@RestController
@RequestMapping("member/dept")
@Tag(name="部门管理")
@AllArgsConstructor
public class MemberDeptController {
    private final MemberDeptService memberDeptService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('member:dept:page')")
    public Result<PageResult<MemberDeptVO>> page(@ParameterObject @Valid MemberDeptQuery query){
        PageResult<MemberDeptVO> page = memberDeptService.page(query);

        return Result.ok(page);
    }

    @GetMapping("list/{orgId}")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('member:dept:page')")
    public Result<List<MemberDeptVO>> list(@PathVariable("orgId") Long orgId){

        return Result.ok(memberDeptService.list(orgId));
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('member:dept:info')")
    public Result<MemberDeptVO> get(@PathVariable("id") Long id){
        MemberDeptEntity entity = memberDeptService.getById(id);

        return Result.ok(MemberDeptConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('member:dept:save')")
    public Result<String> save(@RequestBody MemberDeptVO vo){
        memberDeptService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('member:dept:update')")
    public Result<String> update(@RequestBody @Valid MemberDeptVO vo){
        memberDeptService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('member:dept:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        memberDeptService.delete(idList);

        return Result.ok();
    }
}